﻿
using SlSyncApp.ApplicationService;
using SlSyncLibrary;

namespace SlSyncApp
{
    public class ApplicationServiceClientSync
    {
        ApplicationServiceClient client=new ApplicationServiceClient();

        public ApplicationServiceClientSync()
        {
            client.GetDataCompleted+=GetDataCompleted;
            client.LookupCompleted+=OnLookupCompleted;
        }

        void GetDataCompleted(object sender, GetDataCompletedEventArgs e)
        {
            CallSynchronization<GetDataCompletedEventArgs> syncContext=(CallSynchronization<GetDataCompletedEventArgs>)e.UserState;
            syncContext.Completed(e);
        }

        void OnLookupCompleted(object sender, LookupCompletedEventArgs e)
        {
            CallSynchronization<LookupCompletedEventArgs> syncContext=(CallSynchronization<LookupCompletedEventArgs>)e.UserState;
            syncContext.Completed(e);
        }

        public string GetDataSync(string value)
        {
            CallSynchronization<GetDataCompletedEventArgs> syncContext=new CallSynchronization<GetDataCompletedEventArgs>();
            client.GetDataAsync(value, syncContext);
            syncContext.Wait();
            if (syncContext.Result.Error!=null)
                throw syncContext.Result.Error;
            return syncContext.Result.Result;
        }

        public string LookupDbSync(string value)
        {
            CallSynchronization<LookupCompletedEventArgs> syncContext=new CallSynchronization<LookupCompletedEventArgs>();
            client.LookupAsync(value, syncContext);
            syncContext.Wait();
            if (syncContext.Result.Error!=null)
                throw syncContext.Result.Error;
            return syncContext.Result.Result;
        }
    }
}
